package com.shuoyue.ycgk.ui.estimate;

import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.JobInfo;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface EstimateSelectContract {
    interface View extends BaseView {
        void getUnitSuc(List<String> unit);

        void getAreaSuc(List<String> area);

        void getJobSuc(List<JobInfo> unit);

        void getPaperSuc(UserTestPaper userTestPaper);

        void postSuc();

    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();


        public void getUnit(String keyword, int evaId) {
            apply(model.getUnit(keyword, evaId))
                    .subscribe(new ApiSubscriber<Optional<List<String>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<String>> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.getUnitSuc(listWithPageOptional.getIncludeNull());
                        }
                    });
        }

        public void getAreas(int evaId) {
            apply(model.getAreas(evaId))
                    .subscribe(new ApiSubscriber<Optional<List<String>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<String>> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.getAreaSuc(listWithPageOptional.getIncludeNull());
                        }
                    });
        }

        public void getJob(String areaName, String unitName, int evaId) {
            apply(model.getJob(areaName, unitName, evaId))
                    .subscribe(new ApiSubscriber<Optional<List<JobInfo>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<JobInfo>> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.getJobSuc(listWithPageOptional.getIncludeNull());
                        }
                    });
        }

        public void postChoose(int postId,
                               String postName,
                               int evaId,
                               String unitName) {
            apply(model.registTest(postId, postName, evaId, unitName))
                    .subscribe(new ApiSubscriber<Optional<Object>>(mView, this) {
                        @Override
                        public void onNext(Optional<Object> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            mView.postSuc();
                        }
                    });
        }

        public void getPaper(int testPaperId, int memberEvaId) {
            apply(model.getPaper(testPaperId, memberEvaId))
                    .subscribe(new ApiSubscriber<Optional<UserTestPaper>>(mView, this) {
                        @Override
                        public void onNext(Optional<UserTestPaper> listWithPageOptional) {
                            super.onNext(listWithPageOptional);
                            UserTestPaper u = listWithPageOptional.getIncludeNull();
                            u.setTag(memberEvaId);
                            mView.getPaperSuc(listWithPageOptional.getIncludeNull());

                        }
                    });
        }

    }

    class Model {
        Observable<BaseResult<List<String>>> getUnit(String keyword, int evaId) {
            return RetrofitClient.getInstance().getApi().getUnit(keyword, evaId);
        }

        Observable<BaseResult<List<JobInfo>>> getJob(String areaName, String unitName, int evaId) {
            return RetrofitClient.getInstance().getApi().getJobs(areaName, unitName, evaId);
        }

        Observable<BaseResult<List<String>>> getAreas(int evaId) {
            return RetrofitClient.getInstance().getApi().getEstimatArea(evaId);
        }

        Observable<BaseResult<Object>> registTest(int postId,
                                                  String postName,
                                                  int evaId,
                                                  String unitName) {
            return RetrofitClient.getInstance().getApi().chooseUnitDone(postId, postName, evaId, unitName);
        }

        Observable<BaseResult<UserTestPaper>> getPaper(Integer testPaperId, int evaId) {
            return RetrofitClient.getInstance().getApi().getEstimatePaper(testPaperId, evaId);
        }

    }
}
